import 'package:hex/hex.dart';
final RegExp ADDRESS_PREFIX_REGEX = RegExp(r'^(41)');
const String ADDRESS_PREFIX = '41';


class TronUtil{
  String removeHexPrefix(String hexString) {
    return hexString.startsWith('0x') ? hexString.substring(2) : hexString;
  }

  String removeAddressPrefix(String address) {
    return address.replaceFirst(ADDRESS_PREFIX_REGEX, '');
  }

  String encodeParams(List<Map<String, dynamic>> inputs) {
    final types = <String>[];
    final values = <dynamic>[];

    for (final input in inputs) {
      final type = input['type'] as String;
      var value = input['value'];

      if (type == 'address') {
        value = removeAddressPrefix(value);
      } else if (type == 'address[]') {
        value = (value as List<String>).map(removeAddressPrefix).toList();
      }

      types.add(type);
      values.add(value);
    }

    final encodedParams = values.map((value) {
      if (value is List) {
        final encodedValues = value.map((v) => removeHexPrefix(v)).toList();
        return encodedValues.join('');
      } else {
        print('value $value');
        return removeHexPrefix(value);
      }
    }).join('');
    print('encodedParams $encodedParams');
    final encodedParamsBytes = HEX.decode(encodedParams);
    final encodedParamsHex = HEX.encode(encodedParamsBytes);

    return encodedParamsHex;
  }
}